home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / program / cpp112.zoo / src / include.c < prev    next >
C/C++ Source or Header  |  1994-07-07  |  3KB  |  93 lines

  1.  
  2. /*---------------------------------------------------------------------*\
  3. |                                    |
  4. | CPP -- a stand-alone C preprocessor                    |
  5. | Copyright (c) 1993 Hacker Ltd.        Author: Scott Bigham    |
  6. |                                    |
  7. | Permission is granted to anyone to use this software for any purpose    |
  8. | on any computer system, and to redistribute it freely, with the    |
  9. | following restrictions:                        |
  10. | - No charge may be made other than reasonable charges for repro-    |
  11. |     duction.                                |
  12. | - Modified versions must be clearly marked as such.            |
  13. | - The author is not responsible for any harmful consequences of    |
  14. |     using this software, even if they result from defects therein.    |
  15. |                                    |
  16. | include.c -- handle an #include directive                |
  17. \*---------------------------------------------------------------------*/
  18.  
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include "global.h"
  23.  
  24. unsigned long include_level = 0;
  25.  
  26. /*
  27.    find_include_file() -- look for the include file |fnam| in the list of
  28.    include paths.  If |relative==1|, also check the current directory.  Note:
  29.    |fnam| must be a pointer to malloc()'ed space, which is freed by the
  30.    function.
  31. */
  32. static char *find_include_file(fnam, relative)
  33.   char *fnam;
  34.   int relative;
  35. {
  36.   register char **I = I_list;
  37.   char *buf;
  38.   static size_t buf_len = 20;
  39.   int len;
  40.   register char *s, *t;
  41.  
  42.   if (!relative)
  43.     I++;
  44.   buf = mallok(buf_len);
  45.   for (; *I; I++) {
  46.     len = strlen(*I) + strlen(fnam) + 2;
  47.     if (len > buf_len) {
  48.       buf_len = len;
  49.       buf = reallok(buf, buf_len);
  50.     }
  51.     s = buf;
  52.     t = *I;
  53.     while ((*s++ = *t++))
  54.       continue;
  55.     s[-1] = PATH_SEP;
  56.     t = fnam;
  57.     while ((*s++ = *t++))
  58.       continue;
  59.     if (access(buf, R_OK) == 0)
  60.       break;
  61.   }
  62.   free(fnam);
  63.   return (*I ? reallok(buf, len) : NULL);
  64. }
  65.  
  66. /* do_include() -- handle an #include directive */
  67. void do_include()
  68. {
  69.   char *u;
  70.   register TokenP T;
  71.  
  72.   change_mode(INCLUDE_LINE, 0);
  73.   _tokenize_line();
  74.   T = exp_token();
  75.   change_mode(0, INCLUDE_LINE);
  76.   if (T->type != INC_NAM && T->type != STR_CON)
  77.     fatal("argument %s to #include not a valid filespec", token_txt(T));
  78.   u = copy_filename(token_txt(T) + 1, strlen(token_txt(T)) - 2);
  79.   u = find_include_file(u, (T->type == STR_CON));
  80.   if (!u)
  81.     fatal("cannot find include file %s", token_txt(T));
  82.   free_token(T);
  83.   T = exp_token();
  84.   if (T->type != EOL)
  85.     warning("garbage after #include");
  86.   free_token(T);
  87.   include_level++;
  88.   process_file(u);
  89.   free(u);
  90.   include_level--;
  91.   sync_line(2);
  92. }
  93.